﻿using BaseUtils.Logger;
using Service.src.com.utils.modal;
using Service.src.Flow.Server;
using Service.src.NW.Model;
using System.Collections.Generic;
using System.Web.Http;

namespace NW_WEB_Service.com.app.controllers
{

    [RoutePrefix("api/global-param")]
    public class GlobalParamController : ApiController
    {

        public GlobalParamService ParamServer;

        public GlobalParamController()
        {
            ParamServer = new GlobalParamService();
        }
        /// <summary>
        /// 添加设备，更新设备
        /// </summary>
        /// <param name="GlobalParamJson"></param>
        /// <returns></returns>
        [HttpPost]
        [Route]
        public HttpResult AddGlobalParam([FromBody] GlobalParamEntity GlobalParamInfo)
        {
            if (GlobalParamInfo != null && !string.IsNullOrEmpty(GlobalParamInfo.Id.ToString()) && GlobalParamInfo.Id > 0)
            {
                GlobalParamEntity ParamEntity = new AppData().GetGlobalParam(GlobalParamInfo.Name);
                LoggerService.UpdateDataLog(ParamEntity, GlobalParamInfo, "");
                GlobalParamService.UpldateGlobalParam(GlobalParamInfo);
            }
            else
            {
                LoggerService.UpdateDataLog(GlobalParamInfo, "");
                GlobalParamInfo = ParamServer.AddGlobalParam(GlobalParamInfo);
            }
            AppData.GlobalParams.Clear();
            new AppData().SetGlobalParams();
            return new HttpResult().Success(GlobalParamInfo);
        }

        /// <summary>
        /// 更新全局变量
        /// </summary>
        /// <param name="GlobalParamJson"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("batch")]
        public HttpResult UpdateGlobalParams([FromBody] List<GlobalParamEntity> Updates)
        {
            if (Updates == null)
            {
                return new HttpResult().Error();
            }
            foreach (GlobalParamEntity source in Updates)
            {
                ParamServer.UpdateGlobalParam(source);
            }
            return new HttpResult().Success(Updates);
        }

        [HttpPut]
        [Route]
        public HttpResult UpdateGlobalParam([FromBody] GlobalParamEntity Update)
        {
            bool Res = ParamServer.UpdateGlobalParam(Update);
            return new HttpResult().Success(Res);
        }

        /// <summary>
        /// 根据 ID 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{Id:int}")]
        public HttpResult DeleteGlobalParam(int Id)
        {
            GlobalParamEntity ParamEntity = new AppData().GetGlobalParam(Id);
            LoggerService.UpdateDataLog2(ParamEntity, "");
            bool Res = ParamServer.DeleteGlobalParam(Id);
            AppData.GlobalParams.Clear();
            new AppData().SetGlobalParams();
            return new HttpResult().Success(Res);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route]
        public HttpResult GetGlobalParams()
        {
            return new HttpResult().Success(AppData.GlobalParams);
        }
    }
}
